package com.daniel.gof.observer;

import java.util.LinkedList;
import java.util.List;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/11/19 10:25
 */
public class Weather implements Subject {
    List<Observer> observers;

    float temperature;
    float pressure;
    float humidity;

    public Weather() {
        observers = new LinkedList<>();
    }

    public void changeData(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        notifyObservers();
    }

    @Override
    public void registerObserver(Observer observer) {
        if (!observers.contains(observer)) {
            observers.add(observer);
        }
    }

    @Override
    public void unRegisterObserver(Observer observer) {
        if (observers.contains(observer)) {
            observers.remove(observer);
        }
    }

    @Override
    public void notifyObservers() {
        observers.forEach(s -> s.update(temperature, pressure, humidity));
    }
}
